package org.gm.mybatis.ext.sql;

import java.util.LinkedHashMap;
import java.util.Map;

public class SqlQuery {
	// 动态复杂查询的sql语句
	private StringBuilder sql;
	// 动态sql中的参数集合
	private Map<String, Object> args;
	// 排列
	private String orderString = "";
	// 是否在语句中增加排序
	private boolean existOrder = true;
	
	public String getSql() {
		return sql.toString();
	}

	public void setSql(StringBuilder sql) {
		this.sql = sql;
	}
	
	public void setSql(String sql) {
		this.sql.append(sql);
	}
	
	public void setSql(StringBuffer sql) {
		this.sql.append(sql.toString());
	}

	public Map<String, Object> getArgs() {
		return args;
	}

	public void setArgs(Map<String, Object> args) {
		this.args = args;
	}

	public String getOrderString() {
		return orderString;
	}

	public void setOrderString(String orderString) {
		this.orderString = orderString;
	}

	public boolean isExistOrder() {
		return existOrder;
	}

	public void setExistOrder(boolean existOrder) {
		this.existOrder = existOrder;
	}
	
	private SqlQuery(){
		this.sql = new StringBuilder();
	}
	
	public static SqlQuery createQuery(){
		return new SqlQuery();
	}

	/**
	 * 插入sql片段
	 * @param sqlBuffer
	 * @return
	 */
	public SqlQuery appendSql(String sql){
		this.sql.append(" ")
			.append(sql);
		return this;
	}
	
	/**
	 * 设置查询条件值
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public SqlQuery addArg(String key, Object value) {
		if (this.args == null) {
			this.args = new LinkedHashMap<String, Object>();
		}
		this.args.put(key, value);
		return this;
	}
}